﻿/// <reference path="../../scripts/knockoutjs/knockout-3.2.0.js" />
/// <reference path="../scripts/jquery/jquery-1.11.2.min.js" />


function itemTabla(code, symbol, name, price, currency, changeDay, changeMonth, changeYear) {
    var self = this;
        

    self.code = ko.observable(code.replace(".", ""));
    self.symbol = ko.observable(symbol.replace(".BA", ""));
    self.name = ko.observable(name);
    self.price = ko.observable(price.replace(",", "."));
    self.currency = ko.observable(currency);
    
    //self.priceVariation = ko.observable(priceVariation.replace(",", "."));
    self.changeDay = ko.observable(changeDay.replace(",", "."));
    self.changeMonth = ko.observable(changeMonth.replace(",", "."));
    self.changeYear = ko.observable(changeYear.replace(",", "."));
   
    //self.displayPriceVariation = ko.observable(priceVariation);
    self.displayChangeDay = ko.observable(changeDay);
    self.displayChangeMonth = ko.observable(changeMonth);
    self.displayChangeYear = ko.observable(changeYear);

    //self.htmlPopUp = ko.observable("<div id=\"tooltip" + codigoPopUp + "\" class=\"qtip-content\" ><div > <img src=\"" + urlImagenPopUp  + "\" alt=\"logo\" width=\"100\" height=\"34\" ><div class=\"info\"><h1>" + nombrePopUp + "</h1><p>" + descripcionPopUp + "</p><p class=\"note\">Sitio Web: <a href=\"" + direccionWebPopUp + "\"> </a>" + direccionWebPopUp + "</p><div class=\"clear\"></div></div></div></div>");

    
    switch (symbol) {

        case "PBR":
            {
                self.currency("U$S");
                break;
            }

        case "TS (ADR)":
            {
                self.currency("U$S");
                self.code("TS_ADR");
                break;
            }

        case "YPF (ADR)":
            {
                self.currency("U$S");
                self.code("YPF_ADR");
                break;
            }

        case "GGAL (ADR)":
            {
                self.currency("U$S");
                self.code("GGAL_ADR");
                break;
            }

        case "BMA (ADR)":
            {
                self.currency("U$S");
                self.code("BMA_ADR");
                break;
            }

        case "FRAN (ADR)":
            {
                self.currency("U$S");
                break;
            }

        case "EDN (ADR)":
            {
                self.currency("U$S");
                self.code("EDN_ADR");
                break;
            }

        case "PAMP (ADR)":
            {
                self.currency("U$S");
                break;
            }

        case "TECO2 (ADR)":
            {
                self.currency("U$S");
                self.code("TEO");
                break;
            }

        default:

    }

}

function itemTablaBonos(symbol, name, price, currency, changeDay, changeMonth, changeYear, irr) {
    var self = this;
    
    self.code = ko.observable(symbol.replace(".BA", "").replace("_", ""));
    self.symbol = ko.observable(symbol.replace(".BA", ""));
    self.name = ko.observable(name);
    self.price = ko.observable(price.replace(",", "."));

    if (irr != undefined)
        self.irr = ko.observable(irr.replace(",", "."));
    else
        self.irr = ko.observable("-");

    self.currency = ko.observable(currency);

    //self.priceVariation = ko.observable(priceVariation.replace(",", "."));
    self.changeDay = ko.observable(changeDay.replace(",", "."));
    self.changeMonth = ko.observable(changeMonth.replace(",", "."));
    self.changeYear = ko.observable(changeYear.replace(",", "."));

    //self.displayPriceVariation = ko.observable(priceVariation);
    self.displayChangeDay = ko.observable(changeDay);
    self.displayChangeMonth = ko.observable(changeMonth);
    self.displayChangeYear = ko.observable(changeYear);

    switch (symbol) {
          
        case "AO20D.BA":
            self.currency("U$S");

        case "AA17D.BA":
            self.currency("U$S");

        case "AY24D.BA":
            self.currency("U$S");

        case "AN18D.BA":
            self.currency("U$S");

        case "GJ17D.BA":
            self.currency("U$S");

        case "DIA0D.BA":
            self.currency("U$S");

        case "DICAD.BA":
            self.currency("U$S");

        case "DICYD.BA":
            self.currency("U$S");

        case "DIY0D.BA":
            self.currency("U$S");

        case "PAA0D.BA":
            self.currency("U$S");

        case "PARAD.BA":
            self.currency("U$S");

        case "PARYD.BA":
            self.currency("U$S");

        case "PAY0D.BA":
            self.currency("U$S");

        default:

    }

}


//Función de obtención obtención de objetos que devuelve el webservices
function GetObjetos(URL) {

    $.ajax({
        type: "POST",
        url: "../../../Service1.asmx/GetObjetosPaginas",
        data: "{URL:'" + URL + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {

            GenerarHtml(msg);

        },
        error: AjaxError
    });

}


//Función que genera el html dinámico
function GenerarHtml(Result) {
    var delay = 15000;
    var init = 1;

    var xmlDoc = jsonToXml(Result.d);

    $(xmlDoc).find('Objeto').each(function () {

        var id = $(this).attr('Seccion');
        var html = $(this).attr('html');
        $("div#" + id).html(html);
        
    });
    
    $('div#Sponsor p').hide();

    //$('#Sponsor p:nth-child(' + init + ')').fadeIn(fadetime);
    $('#Sponsor p:nth-child(' + init + ')').show();

    setInterval(function () {
        
        $('#Sponsor p:nth-child(' + init + ')').hide();
        init += 1;
        if ($('#Sponsor p').length < init) {
            init = 1;
        }

        $('#Sponsor p:nth-child(' + init + ')').show();
        
    }, delay);

}

//Función que convierte json a XML
function jsonToXml(string) {
    var browserName = navigator.appName;
    var doc;
    if (browserName == 'Microsoft Internet Explorer') {
        doc = new ActiveXObject('Microsoft.XMLDOM');
        doc.async = 'false'
        doc.loadXML(string);
    } else {
        doc = (new DOMParser()).parseFromString(string, 'text/xml');
    }
    return doc;
}

//Función de error de ajax
function AjaxError(result) {
    //alert("ERROR " + result.status + ' ' + result.statusText);
}

